﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Net;
using System.Globalization;

namespace GoogleMapsAPI
{
    class MapSlate
    {
        private double _longitude;

        public double Longitude
        {
            get { return _longitude; }
            set { _longitude = value; }
        }
        private double _latitude;

        public double Latitude
        {
            get { return _latitude; }
            set { _latitude = value; }
        }
        private int _zoom;

        public int Zoom
        {
            get { return _zoom; }
            set { _zoom = value; }
        }

        public MapSlate()
        {
            _longitude = 107;
            _latitude = 11;
            _zoom = 9;
        }

        public Bitmap GetMapImage()
        {
            string url = "http://maps.google.com/maps/api/staticmap?center=" + _latitude.ToString("f6", new CultureInfo("en-us")) + "," + _longitude.ToString("f6", new CultureInfo("en-us")) + "&zoom=" + _zoom + "&size=256x256&sensor=false";
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            return new Bitmap(response.GetResponseStream());
        }

        public static Bitmap GetMapImage(double lat, double lng, int zoom, int size)
        {
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat("http://maps.google.com/maps/api/staticmap?center={0},{1}&zoom={2}&size={3}x{3}&sensor=false", lat.ToString("f6", new CultureInfo("en-us")), lng.ToString("f6", new CultureInfo("en-us")), zoom, size);
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sb.ToString());
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                return new Bitmap(response.GetResponseStream());
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public static Bitmap GetMapImage(double lat, double lng, int zoom, int size, List<Data> data)
        {
            try
            {
                String mark = "";
                foreach (Data d in data)
                    mark += "&markers=color:green|" + d.Lat + "," + d.Lng;
                
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat("http://maps.google.com/maps/api/staticmap?center={0},{1}&zoom={2}&size={3}x{3}{4}&sensor=false", lat.ToString("f6", new CultureInfo("en-us")), lng.ToString("f6", new CultureInfo("en-us")), zoom, size, mark);
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sb.ToString());
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                return new Bitmap(response.GetResponseStream());
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public static Bitmap GetCacheImage(double lat, double lng, int zoom, int size)
        {
            return new Bitmap(1, 1);
        }
    }
}
